#include "SceneExporter.h";

SceneExporter::SceneExporter(void)
{
	nCurrentSeq = 99;
}

//-----------------------------------------------------------

SceneExporter::~SceneExporter(void)
{
}

//----------------------------------------------------------------------------

void SceneExporter::init(Ogre::String saveDBPath)
{
	saveDB.Clear();

	bool loadOkay = saveDB.LoadFile(saveDBPath.data());

	if (loadOkay)
	{
		std::cout << "\n<SceneImporter>: file loaded, processing...";

		TiXmlElement *root = saveDB.FirstChildElement();

		if(root != 0)
		{
			TiXmlElement *seqNode = root->FirstChildElement("seq");

			if(seqNode != 0)
			{
				nCurrentSeq = Ogre::StringConverter::parseInt(seqNode->GetText());
			}
		}
	}
}

//-----------------------------------------------------------------

void SceneExporter::updateSaveDB(Ogre::String fileName, Ogre::String name, Ogre::String description)
{
		TiXmlElement *root = saveDB.FirstChildElement();

		if(root != 0)
		{
			TiXmlElement *seqNode = root->FirstChildElement("seq");

			if(seqNode != 0)
			{
				seqNode->Clear();
				seqNode->LinkEndChild(new TiXmlText(Ogre::StringConverter::toString(nCurrentSeq).data()));
			}


			TiXmlElement *saveFileNode = new TiXmlElement( "SaveFile" ); 

			TiXmlElement *nameNode = new TiXmlElement( "Name" );
				nameNode->LinkEndChild(new TiXmlText(name.data()));
			TiXmlElement *descriptionNode = new TiXmlElement( "Description" );
				descriptionNode->LinkEndChild(new TiXmlText(description.data()));
			TiXmlElement *fileNameNode = new TiXmlElement( "FileName" );
				fileNameNode->LinkEndChild(new TiXmlText(fileName.data()));

			saveFileNode->LinkEndChild(nameNode);
			saveFileNode->LinkEndChild(descriptionNode);
			saveFileNode->LinkEndChild(fileNameNode);

			root->LinkEndChild(saveFileNode);
		}


	saveDB.SaveFile();
}

//----------------------------------------------------------

void SceneExporter::exportSceneToXml(Ogre::String name, Ogre::String description, PredefinedCameraDescriptions *mPredefinedCameraDescriptions, Map *mMap, WeatherSystem *mWeatherSystem, PhysXWorld *mPhysXWorld)
{
	Ogre::String value = "";

	TiXmlElement *root = new TiXmlElement( "SaveFile" ); 
	nCurrentSeq++;

	
		TiXmlElement *camDescriptions = new TiXmlElement( "CameraDescriptions" ); 

					int ccount = mPredefinedCameraDescriptions->lCameraDescriptions.size();
					for(int i=0;i<ccount;i++)
					{

						TiXmlElement *camDescription = new TiXmlElement( "CameraDescription" );

						CameraDescription *descr = mPredefinedCameraDescriptions->lCameraDescriptions.at(i);

								TiXmlElement *camString = new TiXmlElement( "CameraString" ); 

								value = descr->getDescription();
								camString->LinkEndChild( new TiXmlText( value.data() ) );

								camDescription->LinkEndChild(camString);


								TiXmlElement *camPoz = new TiXmlElement( "CameraPoz" ); 

								value = Ogre::StringConverter::toString(descr->getPosition());
								camPoz->LinkEndChild( new TiXmlText( value.data() ) );

								camDescription->LinkEndChild(camPoz);


								TiXmlElement *camRot = new TiXmlElement( "CameraRot" ); 

								value = Ogre::StringConverter::toString(descr->getOrientation());
								camRot->LinkEndChild( new TiXmlText( value.data() ) );

								camDescription->LinkEndChild(camRot);

						camDescriptions->LinkEndChild(camDescription);

					}


		root->LinkEndChild(camDescriptions);



		TiXmlElement *mapData = new TiXmlElement( "MapData" ); 


				TiXmlElement *heightMap = new TiXmlElement( "HeightMap" ); 
					
					Ogre::String heightMapName = "saveHeightMap";
					heightMapName.append(Ogre::StringConverter::toString(nCurrentSeq));
					heightMapName.append(".bmp");

					mMap->generateHeightMap(heightMapName);
					heightMap->LinkEndChild( new TiXmlText( heightMapName.data() ) );

				mapData->LinkEndChild(heightMap);


		root->LinkEndChild(mapData);

		



	    TiXmlElement *weatherData = new TiXmlElement( "WeatherData" ); 



					TiXmlElement *dayTime = new TiXmlElement( "DayTime" ); 

						WeatherSystem::eDayTime time = mWeatherSystem->getDayTime();

						switch(time)
						{
						case WeatherSystem::DayTime_Dawn:
							value = "Dawn";
							break;
						case WeatherSystem::DayTime_Noon:
							value = "Noon";
							break;
						case WeatherSystem::DayTime_AfterNoon:
							value = "AfterNoon";
							break;
						case WeatherSystem::DayTime_SunSet:
							value = "SunSet";
							break;
						case WeatherSystem::DayTime_Night:
							value = "Night";
							break;
						}

						dayTime->LinkEndChild( new TiXmlText( value.data() ) );
					weatherData->LinkEndChild(dayTime);



				
					TiXmlElement *isRaining = new TiXmlElement( "isRaining" ); 

						value = Ogre::StringConverter::toString(mWeatherSystem->isRaining());
						isRaining->LinkEndChild( new TiXmlText( value.data() ) );

					weatherData->LinkEndChild(isRaining);



					TiXmlElement *isSnowing = new TiXmlElement( "isSnowing" ); 

						value = Ogre::StringConverter::toString(mWeatherSystem->isSnowing());
						isSnowing->LinkEndChild( new TiXmlText( value.data() ) );

					weatherData->LinkEndChild(isSnowing);



					TiXmlElement *snowLevel = new TiXmlElement( "SnowLevel" ); 

						value = Ogre::StringConverter::toString(mWeatherSystem->getSnowLevel());
						snowLevel->LinkEndChild( new TiXmlText( value.data() ) );

					weatherData->LinkEndChild(snowLevel);




		root->LinkEndChild(weatherData);





		TiXmlElement *objectData = new TiXmlElement( "ObjectData" ); 

			int count = mPhysXWorld->lActors->size();

			for(int i=0;i<count;i++)
			{
				exportActorToXml(objectData,mPhysXWorld->lActors->at(i));
			}

			
		root->LinkEndChild(objectData);


	saveFile.LinkEndChild(root);


	Ogre::String fileName = "SaveFiles/savefile";
	Ogre::String seqString = Ogre::StringConverter::toString(nCurrentSeq);
	fileName.append(seqString);
	fileName.append(".xml");

	saveFile.SaveFile(fileName.data());
	updateSaveDB(fileName, name, description);
}

//----------------------------------------------------------

void SceneExporter::exportActorToXml(TiXmlElement *parentNode, ActorEntitySceneNode *actor)
{
	Ogre::String value = "";


		TiXmlElement *Actor = new TiXmlElement( "Actor" ); 

					TiXmlElement *actorName = new TiXmlElement( "Name" ); 
					
					    value = actor->getName();
						actorName->LinkEndChild( new TiXmlText( value.data() ) );

					Actor->LinkEndChild(actorName);


					TiXmlElement *actorType = new TiXmlElement( "Type" ); 
					
						WorldEnums::eSceneryObjects type = actor->getSceneryId();

						switch(type)
						{
						case WorldEnums::Scenery_Block1:
							value = "Block1";
							break;
						case WorldEnums::Scenery_Block2:
							value = "Block2";
							break;
						case WorldEnums::Scenery_Block3:
							value = "Block3";
							break;
						case WorldEnums::Scenery_House1:
							value = "House1";
							break;
						case WorldEnums::Scenery_House2:
							value = "House2";
							break;
						case WorldEnums::Scenery_Tree1:
							value = "Tree1";
							break;
						case WorldEnums::Scenery_Tree2:
							value = "Tree2";
							break;
						case WorldEnums::Scenery_Lake1:
							value = "Lake1";
							break;
						case WorldEnums::Scenery_Lake2:
							value = "Lake2";
							break;
						case WorldEnums::Scenery_Lake3:
							value = "Lake3";
							break;
						}

						actorType->LinkEndChild( new TiXmlText( value.data() ) );
					Actor->LinkEndChild(actorType);



					TiXmlElement *actorPoz = new TiXmlElement( "ActorPoz" ); 
					
					    value = Ogre::StringConverter::toString(actor->getSceneNode()->getPosition());
						actorPoz->LinkEndChild( new TiXmlText( value.data() ) );

					Actor->LinkEndChild(actorPoz);


					TiXmlElement *actorRot = new TiXmlElement( "ActorRot" ); 
					
					    value = Ogre::StringConverter::toString(actor->getSceneNode()->getOrientation());
						actorRot->LinkEndChild( new TiXmlText( value.data() ) );

					Actor->LinkEndChild(actorRot);


					TiXmlElement *actorScale = new TiXmlElement( "ActorScale" ); 
					
					    value = Ogre::StringConverter::toString(actor->getSceneNode()->getScale());
						actorScale->LinkEndChild( new TiXmlText( value.data() ) );

					Actor->LinkEndChild(actorScale);

				
					if(actor->getActorType() == ActorEntitySceneNode::Emitter_Holder)
					{
						Lake *lake = (Lake*)actor;

						TiXmlElement *actorCapacity = new TiXmlElement( "ActorCapacity" ); 
					
							value = Ogre::StringConverter::toString(lake->getCapacity());
							actorCapacity->LinkEndChild( new TiXmlText( value.data() ) );

						Actor->LinkEndChild(actorCapacity);


						TiXmlElement *actorLevel = new TiXmlElement( "ActorLevel" ); 
					
							value = Ogre::StringConverter::toString(lake->getCurrentLevel());
							actorLevel->LinkEndChild( new TiXmlText( value.data() ) );

						Actor->LinkEndChild(actorLevel);


						TiXmlElement *attachedEmitters = new TiXmlElement( "AttachedEmitters" ); 
					
							int emitterCount = lake->getNumEmitters();
							for(int i = 0;i<emitterCount;i++)
							{
								TiXmlElement *emitter = new TiXmlElement( "Emitter" );

								Ogre::Vector3 position = Ogre::Vector3(lake->getEmitter(i)->getPosition().x,lake->getEmitter(i)->getPosition().y,lake->getEmitter(i)->getPosition().z);
								Ogre::String name = lake->getEmitter(i)->getName();

									TiXmlElement *emitterName = new TiXmlElement( "Name" );
									value = name;
									emitterName->LinkEndChild( new TiXmlText( value.data() ) );

									TiXmlElement *emitterPoz = new TiXmlElement( "Poz" );
									value = Ogre::StringConverter::toString(position);
									emitterPoz->LinkEndChild( new TiXmlText( value.data() ) );

									emitter->LinkEndChild(emitterName);
									emitter->LinkEndChild(emitterPoz);

								attachedEmitters->LinkEndChild(emitter);
							}
						

						Actor->LinkEndChild(attachedEmitters);
					}

		parentNode->LinkEndChild(Actor);
	
}

//----------------------------------------------------------